home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / rpclib / clnt_perror.c < prev    next >
C/C++ Source or Header  |  1994-05-02  |  7KB  |  321 lines

  1. /*
  2.  * $Id: clnt_perror.c,v 1.5 1994/05/02 20:53:13 jraja Exp $
  3.  *
  4.  * $Log: clnt_perror.c,v $
  5.  * Revision 1.5  1994/05/02  20:53:13  jraja
  6.  * Removed the __sys_nerr define (moved to <errno.h>).
  7.  *
  8.  * Revision 1.4  1994/05/02  19:37:20  jraja
  9.  * Added HACK to fix the need for the __sys_nerr (a define to 80).
  10.  *
  11.  * Revision 1.3  1994/02/25  00:21:41  jraja
  12.  * Changed malloc, calloc and free to mem_alloc, mem_calloc and mem_free,
  13.  * respectively.
  14.  *
  15.  * Revision 1.2  1993/11/10  01:30:07  jraja
  16.  * Fixed includes, added ANSI prototypes.
  17.  *
  18.  */
  19. /* @(#)clnt_perror.c    2.1 88/07/29 4.0 RPCSRC */
  20. /*
  21.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  22.  * unrestricted use provided that this legend is included on all tape
  23.  * media and as a part of the software program in whole or part.  Users
  24.  * may copy or modify Sun RPC without charge, but are not authorized
  25.  * to license or distribute it to anyone else except as part of a product or
  26.  * program developed by the user.
  27.  * 
  28.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  29.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  30.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  31.  * 
  32.  * Sun RPC is provided with no support and without any obligation on the
  33.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  34.  * modification or enhancement.
  35.  * 
  36.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  37.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  38.  * OR ANY PART THEREOF.
  39.  * 
  40.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  41.  * or profits or other special, indirect and consequential damages, even if
  42.  * Sun has been advised of the possibility of such damages.
  43.  * 
  44.  * Sun Microsystems, Inc.
  45.  * 2550 Garcia Avenue
  46.  * Mountain View, California  94043
  47.  */
  48. #if !defined(lint) && defined(SCCSIDS)
  49. static char sccsid[] = "@(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro";
  50. #endif
  51.  
  52. /*
  53.  * clnt_perror.c
  54.  *
  55.  * Copyright (C) 1984, Sun Microsystems, Inc.
  56.  *
  57.  */
  58. #include <sys/param.h>
  59. #include <rpc/types.h>
  60. #include <rpc/xdr.h>
  61. #include <rpc/auth.h>
  62. #include <rpc/clnt.h>
  63. #include <stdio.h>
  64. #include <errno.h>
  65.  
  66.  
  67. static char *auth_errmsg(enum auth_stat stat);
  68.  
  69. static char *buf;
  70.  
  71. static char *
  72. _buf(void)
  73. {
  74.  
  75.     if (buf == 0)
  76.         buf = (char *)mem_alloc(256);
  77.     return (buf);
  78. }
  79.  
  80. /*
  81.  * Print reply error info
  82.  */
  83. char *
  84. clnt_sperror(rpch, s)
  85.     CLIENT *rpch;
  86.     char *s;
  87. {
  88.     struct rpc_err e;
  89.     void clnt_perrno();
  90.     char *err;
  91.     char *str = _buf();
  92.     char *strstart = str;
  93.  
  94.     if (str == 0)
  95.         return (0);
  96.     CLNT_GETERR(rpch, &e);
  97.  
  98.     (void) sprintf(str, "%s: ", s);  
  99.     str += strlen(str);
  100.  
  101.     (void) strcpy(str, clnt_sperrno(e.re_status));  
  102.     str += strlen(str);
  103.  
  104.     switch (e.re_status) {
  105.     case RPC_SUCCESS:
  106.     case RPC_CANTENCODEARGS:
  107.     case RPC_CANTDECODERES:
  108.     case RPC_TIMEDOUT:     
  109.     case RPC_PROGUNAVAIL:
  110.     case RPC_PROCUNAVAIL:
  111.     case RPC_CANTDECODEARGS:
  112.     case RPC_SYSTEMERROR:
  113.     case RPC_UNKNOWNHOST:
  114.     case RPC_UNKNOWNPROTO:
  115.     case RPC_PMAPFAILURE:
  116.     case RPC_PROGNOTREGISTERED:
  117.     case RPC_FAILED:
  118.         break;
  119.  
  120.     case RPC_CANTSEND:
  121.     case RPC_CANTRECV:
  122.         (void) sprintf(str, "; errno = %s",
  123.             strerror(e.re_errno)); 
  124.         str += strlen(str);
  125.         break;
  126.  
  127.     case RPC_VERSMISMATCH:
  128.         (void) sprintf(str,
  129.             "; low version = %lu, high version = %lu", 
  130.             e.re_vers.low, e.re_vers.high);
  131.         str += strlen(str);
  132.         break;
  133.  
  134.     case RPC_AUTHERROR:
  135.         err = auth_errmsg(e.re_why);
  136.         (void) sprintf(str,"; why = ");
  137.         str += strlen(str);
  138.         if (err != NULL) {
  139.             (void) sprintf(str, "%s",err);
  140.         } else {
  141.             (void) sprintf(str,
  142.                 "(unknown authentication error - %d)",
  143.                 (int) e.re_why);
  144.         }
  145.         str += strlen(str);
  146.         break;
  147.  
  148.     case RPC_PROGVERSMISMATCH:
  149.         (void) sprintf(str, 
  150.             "; low version = %lu, high version = %lu", 
  151.             e.re_vers.low, e.re_vers.high);
  152.         str += strlen(str);
  153.         break;
  154.  
  155.     default:    /* unknown */
  156.         (void) sprintf(str, 
  157.             "; s1 = %lu, s2 = %lu", 
  158.             e.re_lb.s1, e.re_lb.s2);
  159.         str += strlen(str);
  160.         break;
  161.     }
  162.     (void) sprintf(str, "\n");
  163.     return(strstart) ;
  164. }
  165.  
  166. void
  167. clnt_perror(rpch, s)
  168.     CLIENT *rpch;
  169.     char *s;
  170. {
  171.     (void) fprintf(stderr,"%s",clnt_sperror(rpch,s));
  172. }
  173.  
  174.  
  175. struct rpc_errtab {
  176.     enum clnt_stat status;
  177.     char *message;
  178. };
  179.  
  180. static struct rpc_errtab  rpc_errlist[] = {
  181.     { RPC_SUCCESS, 
  182.         "RPC: Success" }, 
  183.     { RPC_CANTENCODEARGS, 
  184.         "RPC: Can't encode arguments" },
  185.     { RPC_CANTDECODERES, 
  186.         "RPC: Can't decode result" },
  187.     { RPC_CANTSEND, 
  188.         "RPC: Unable to send" },
  189.     { RPC_CANTRECV, 
  190.         "RPC: Unable to receive" },
  191.     { RPC_TIMEDOUT, 
  192.         "RPC: Timed out" },
  193.     { RPC_VERSMISMATCH, 
  194.         "RPC: Incompatible versions of RPC" },
  195.     { RPC_AUTHERROR, 
  196.         "RPC: Authentication error" },
  197.     { RPC_PROGUNAVAIL, 
  198.         "RPC: Program unavailable" },
  199.     { RPC_PROGVERSMISMATCH, 
  200.         "RPC: Program/version mismatch" },
  201.     { RPC_PROCUNAVAIL, 
  202.         "RPC: Procedure unavailable" },
  203.     { RPC_CANTDECODEARGS, 
  204.         "RPC: Server can't decode arguments" },
  205.     { RPC_SYSTEMERROR, 
  206.         "RPC: Remote system error" },
  207.     { RPC_UNKNOWNHOST, 
  208.         "RPC: Unknown host" },
  209.     { RPC_UNKNOWNPROTO,
  210.         "RPC: Unknown protocol" },
  211.     { RPC_PMAPFAILURE, 
  212.         "RPC: Port mapper failure" },
  213.     { RPC_PROGNOTREGISTERED, 
  214.         "RPC: Program not registered"},
  215.     { RPC_FAILED, 
  216.         "RPC: Failed (unspecified error)"}
  217. };
  218.  
  219.  
  220. /*
  221.  * This interface for use by clntrpc
  222.  */
  223. char *
  224. clnt_sperrno(stat)
  225.     enum clnt_stat stat;
  226. {
  227.     int i;
  228.  
  229.     for (i = 0; i < sizeof(rpc_errlist)/sizeof(struct rpc_errtab); i++) {
  230.         if (rpc_errlist[i].status == stat) {
  231.             return (rpc_errlist[i].message);
  232.         }
  233.     }
  234.     return ("RPC: (unknown error code)");
  235. }
  236.  
  237. void
  238. clnt_perrno(num)
  239.     enum clnt_stat num;
  240. {
  241.     (void) fprintf(stderr,"%s",clnt_sperrno(num));
  242. }
  243.  
  244.  
  245. char *
  246. clnt_spcreateerror(s)
  247.     char *s;
  248. {
  249.     char *str = _buf();
  250.  
  251.     if (str == 0)
  252.         return(0);
  253.     (void) sprintf(str, "%s: ", s);
  254.     (void) strcat(str, clnt_sperrno(rpc_createerr.cf_stat));
  255.     switch (rpc_createerr.cf_stat) {
  256.     case RPC_PMAPFAILURE:
  257.         (void) strcat(str, " - ");
  258.         (void) strcat(str,
  259.             clnt_sperrno(rpc_createerr.cf_error.re_status));
  260.         break;
  261.  
  262.     case RPC_SYSTEMERROR:
  263.         (void) strcat(str, " - ");
  264.         if (rpc_createerr.cf_error.re_errno > 0
  265.             && rpc_createerr.cf_error.re_errno < __sys_nerr)
  266.             (void) strcat(str,
  267.                 strerror(rpc_createerr.cf_error.re_errno));
  268.         else
  269.             (void) sprintf(&str[strlen(str)], "Error %d",
  270.                 rpc_createerr.cf_error.re_errno);
  271.         break;
  272.     }
  273.     (void) strcat(str, "\n");
  274.     return (str);
  275. }
  276.  
  277. void
  278. clnt_pcreateerror(s)
  279.     char *s;
  280. {
  281.     (void) fprintf(stderr,"%s",clnt_spcreateerror(s));
  282. }
  283.  
  284. struct auth_errtab {
  285.     enum auth_stat status;    
  286.     char *message;
  287. };
  288.  
  289. static struct auth_errtab auth_errlist[] = {
  290.     { AUTH_OK,
  291.         "Authentication OK" },
  292.     { AUTH_BADCRED,
  293.         "Invalid client credential" },
  294.     { AUTH_REJECTEDCRED,
  295.         "Server rejected credential" },
  296.     { AUTH_BADVERF,
  297.         "Invalid client verifier" },
  298.     { AUTH_REJECTEDVERF,
  299.         "Server rejected verifier" },
  300.     { AUTH_TOOWEAK,
  301.         "Client credential too weak" },
  302.     { AUTH_INVALIDRESP,
  303.         "Invalid server verifier" },
  304.     { AUTH_FAILED,
  305.         "Failed (unspecified error)" },
  306. };
  307.  
  308. static char *
  309. auth_errmsg(stat)
  310.     enum auth_stat stat;
  311. {
  312.     int i;
  313.  
  314.     for (i = 0; i < sizeof(auth_errlist)/sizeof(struct auth_errtab); i++) {
  315.         if (auth_errlist[i].status == stat) {
  316.             return(auth_errlist[i].message);
  317.         }
  318.     }
  319.     return(NULL);
  320. }
  321.